package com.bilin.coin.network.builder;


import com.bilin.coin.network.RetrofitConfig;
import com.bilin.coin.network.api.RetrofitApi;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.SharedPreferencesUtils;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * Created by Administrator on 2018/3/28.
 */

public class PostJsonBuilder extends RetrofitRequestBuilder<String, PostJsonBuilder>{

    //String jsonParam = "{}";
    RequestBody jsonParam;

    public <T> PostJsonBuilder(T param) {
        jsonParam = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtil.GsonString(param));
    }


    public  PostJsonBuilder(String param) {
        jsonParam = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), param);
    }

    public  PostJsonBuilder(String param,String baseUrl) {
        this.baseUrl = baseUrl;
        jsonParam = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), param);
    }

    @Override
    public Observable<String> build() {
        String cookie = SharedPreferencesUtils.getInstance().getString("Cookie", "");
        Map<String, Object> mapCookie = GsonUtil.GsonToMaps(cookie);
        StringBuffer buffer = new StringBuffer();
        if(mapCookie!=null) {
            for (String key : mapCookie.keySet()) {
                buffer.append(key).append("=").append(mapCookie.get(key)).append("; ");
            }
        }
        //headers.put("Cookie", buffer.toString() + "lang="+ SharedPreferencesUtilsHX.getInstance().getString("lang","cn")+";Path=/");

        //headers.put(ApiConstant.DeviceId, SharedPreferencesUtilsHX.getInstance().getString(ApiConstant.DeviceId, ""));

        return RetrofitConfig
                .initRetrofitConfig()
                .baseUrl(baseUrl)
                .build()
                .create(RetrofitApi.class)
                .post(url,headers,jsonParam)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

}
